package main

import (
	"minicommit/internal/app"
	"minicommit/internal/config"
	"os"

	"github.com/rs/zerolog"
	"github.com/rs/zerolog/log"
)

func main() {
	file, err := os.OpenFile(
		"log.txt",
		os.O_APPEND|os.O_CREATE|os.O_WRONLY,
		0664,
	)

	if err != nil {
		panic(err)
	}

	defer file.Close()

	logger := zerolog.New(file).With().Timestamp().Logger()
	if err := config.ReadConfigYML("config.yml"); err != nil {
		logger.Fatal().Err(err).Msg("Failed init configuration")
	}

	cfg := config.GetConfigInstance()

	logger.Info().
		Str("version", cfg.Project.Version).
		Str("commitHash", cfg.Project.CommitHash).
		Bool("debug", cfg.Project.Debug).
		Str("environment", cfg.Project.Environment).
		Msgf("Starting service: %s", cfg.Project.Name)

	app := app.New(cfg.Status.Port,
		cfg.JobProject.DirRepos,
		cfg.Project.Version,
		cfg.Project.Name,
		cfg.Project.BranchName)

	app.AdminID = cfg.Bot.AdminID
	app.BotToken = cfg.Bot.Token

	err = app.Start()
	if err != nil {
		log.Fatal().AnErr("Start app", err)
	}
}
